package com.skivingcloud.admin.sys.service.impl;

import com.skivingcloud.admin.sys.entity.SysConfig;
import com.skivingcloud.admin.sys.entity.User;
import com.skivingcloud.admin.sys.mapper.SysConfigMapper;
import com.skivingcloud.admin.sys.service.ISysConfigService;
import com.skivingcloud.common.contants.Constant;
import com.skivingcloud.common.exception.BusinessException;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.StringUtils;
import com.skivingcloud.common.utils.UuidUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.sf.json.JSONObject;

/**
 * <p>
 * 系统参数配置信息表 服务实现类
 * </p>
 *
 * @author skivingcloud
 * @since 2022-01-29
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements ISysConfigService {


	/*
	 * （非 Javadoc）
	 * queryPage
	 * 
	 * @param params
	 * 
	 * @return
	 * 
	 * @see com.rf.cloud.annuity.sys.service.ConfigService#queryPage(java.util.Map)
	 */
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		PageUtils pageUtils = PageUtils.getPageInfo(params);
		Page<SysConfig> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
		SysConfig config = (SysConfig) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")), 
				SysConfig.class);
		QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<SysConfig>();
		
		if (StringUtils.isNotBlank(config.getParamKey())) {
			queryWrapper.like("param_key", "%" + config.getParamKey().trim() + "%");
		}
		if (StringUtils.isNotBlank(config.getParamValue())) {
			queryWrapper.like("param_value", "%" + config.getParamValue().trim() + "%");
		}
		if (StringUtils.isNotBlank(config.getRemark())) {
			queryWrapper.like("remark", "%" + config.getRemark().trim() + "%");
		}
		queryWrapper.orderByAsc("param_key");
		List<SysConfig> list = this.baseMapper.selectList(queryWrapper);
		pageUtils = new PageUtils(p);
		return pageUtils;
	}

	/*
	 * （非 Javadoc）
	 * save
	 * 
	 * @param config
	 * 
	 * @see
	 * com.rf.cloud.annuity.sys.service.ConfigService#save(com.rf.cloud.annuity.sys.
	 * model.Config)
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(SysConfig config, User user) {
		boolean hasParamkey = checkHasParamkey(config.getParamKey());
		if (hasParamkey) {
			throw new BusinessException("参数编码已被占用");
		}
		LocalDateTime date = LocalDateTime.now();
		config.setId(UuidUtil.get32UUID());
		config.setCreator(user.getId());
		config.setCreateTime(date);
		config.setModUser(user.getId());
		config.setModTime(date);
		this.baseMapper.insert(config);
	}

	/**
	 * 校验paramKey是否重复
	 * 
	 * @param paramKey
	 * @return
	 */
	public boolean checkHasParamkey(String paramKey) {
		QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<SysConfig>();
		queryWrapper.eq("param_key", paramKey);
		List<SysConfig> list = this.baseMapper.selectList(queryWrapper);
		if (list.size() > 0) {
			return true;
		}
		return false;
	}

	/*
	 * （非 Javadoc）
	 * update
	 * 
	 * @param config
	 * 
	 * @see
	 * com.rf.cloud.annuity.sys.service.ConfigService#update(com.rf.cloud.annuity.
	 * sys.model.Config)
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(SysConfig config, User user) {
		LocalDateTime date = LocalDateTime.now();
		config.setCreator(user.getId());
		config.setCreateTime(date);
		config.setModUser(user.getId());
		config.setModTime(date);
		this.baseMapper.updateById(config);
	}

	/*
	 * （非 Javadoc）
	 * deleteBatch
	 * 
	 * @param ids
	 * 
	 * @see
	 * com.rf.cloud.annuity.sys.service.ConfigService#deleteBatch(java.lang.String[]
	 * )
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(String[] ids) {
		this.baseMapper.deleteBatchIds(Arrays.asList(ids));
	}

	/*
	 * （非 Javadoc）
	 * selectById
	 * 
	 * @param id
	 * 
	 * @return
	 * 
	 * @see
	 * com.rf.cloud.annuity.sys.service.ConfigService#selectById(java.lang.String)
	 */
	@Override
	public SysConfig selectById(String id) {
		return this.baseMapper.selectById(id);
	}

	/*
	 * （非 Javadoc）
	 * getParamVlueByKey
	 * 
	 * @param paramKey
	 * 
	 * @return
	 * 
	 * @see
	 * com.rf.cloud.annuity.sys.service.ConfigService#getParamVlueByKey(java.lang.
	 * String)
	 */
	@Override
	public SysConfig getParamVlueByKey(String paramKey) {
		QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<SysConfig>();
		queryWrapper.eq("param_key", paramKey);
		queryWrapper.eq("status", Constant.COMMON_STATUS_NORMAL);
		List<SysConfig> list = this.baseMapper.selectList(queryWrapper);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
}
